//
//  MCStoreDetailTopView.swift
//  StoreManage
//
//  Created by MC on 2017/12/26.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCStoreDetailTopView: UIView {

    var dataDict = Dictionary<String,Any>() {
        didSet {
            // 店铺信息
            let store = dataDict["store"] as! [String : AnyObject]
            let money = store["money"] as! NSNumber
            let logoPath = store["logoPath"] as! String
            let storeName = store["storeName"] as! String
            
            
            
            let arrayM = NSMutableArray()
            let images = dataDict["images"] as? NSArray ?? []
            for dict in images {
                let tempDict = dict as? [String:Any] ?? [:]
                let imgUrl = tempDict["imgUrl"] as? String ?? ""
                arrayM.add(imgUrl)
            }
            
            if arrayM.count > 0 {
                cycleScrollView.imageURLStringsGroup = arrayM as! [Any]
            } else {
                cycleScrollView.localizationImageNamesGroup = [#imageLiteral(resourceName: "Store_bg")]
            }
            
            
            
            iconImageView.sd_setImage(with: URL.init(string: logoPath), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            nameLabel.text = storeName
            moneyLabel.text = "门店余额:" + MCString_judgemantDecimalsIsZero(figure: money.intValue)
        }
    }
        
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(cycleScrollView)
        cycleScrollView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(MCScreenWidth/2)
        }
        
        self.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.top.equalTo(cycleScrollView.snp.bottom).offset(-35)
            make.size.equalTo(CGSize.init(width: 80, height: 80))
        }
        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(20)
            make.top.equalTo(cycleScrollView.snp.bottom).offset(10)
        }
        
//        self.addSubview(moneyLabel)
//        moneyLabel.snp.remakeConstraints { (make) ->Void in
//            make.left.equalTo(self).offset(15)
//            make.top.equalTo(iconImageView.snp.bottom).offset(25)
//        }
//        
//        // 只有店长才可以提现
//        if MCUserinfo_getLevel() == MCRole_manager {
//            self.addSubview(drawMoneyButton)
//            drawMoneyButton.snp.remakeConstraints { (make) ->Void in
//                make.right.equalTo(-20)
//                make.centerY.equalTo(moneyLabel)
//                make.size.equalTo(CGSize.init(width: 60, height: 25))
//            }
//        }
        
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(7)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    lazy var bgImageView: UIImageView = {
//        let iv = UIImageView()
//        iv.contentMode = UIViewContentMode.scaleAspectFill
//        return iv
//    }()
    
    lazy var cycleScrollView: SDCycleScrollView = {

        let cycleScrollView : SDCycleScrollView?  = SDCycleScrollView.init()
        cycleScrollView?.delegate = self as SDCycleScrollViewDelegate
        cycleScrollView?.scrollDirection = UICollectionViewScrollDirection.horizontal
        cycleScrollView?.autoScrollTimeInterval = 3
        cycleScrollView?.infiniteLoop = false
        cycleScrollView?.backgroundColor = UIColor.white
        cycleScrollView?.bannerImageViewContentMode = UIViewContentMode.scaleAspectFill
        cycleScrollView?.layer.masksToBounds = true
        cycleScrollView?.showPageControl = false
        cycleScrollView?.placeholderImage = #imageLiteral(resourceName: "Store_bg")
        return cycleScrollView!
    }()
    
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 40
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1.5
        return imageView
    }()

    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(18)
        return label
    }()
    
    lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.black
        label.text = "门店余额: ¥0"
        return label
    }()
    
    lazy var drawMoneyButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor.MCMain.cgColor
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.setTitle("提现", for: UIControlState.normal)
        return button
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}

extension MCStoreDetailTopView : SDCycleScrollViewDelegate {
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        
    }
}
